/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.executableTasks;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.location.Location;
import javax.microedition.location.LocationException;

import com.aroundj2me.location.LocationService;
import com.aroundj2me.model.ExecutableTask;
import com.aroundj2me.model.Messages;
import com.aroundj2me.utility.StringUtils;

public class PlaceFinder extends ExecutableTask {
	private String category;
	private LocationService locationService;
	private String searchCriteria;
	
	public PlaceFinder(LocationService locationService) {
		this.locationService = locationService;
	}
	
	public void run() {
		super.run();
		
		try {
			Location currentPosition = locationService.getCurrentLocation();

			Vector places = null;
			if(!StringUtils.isNullOrEmpty(searchCriteria)){
				places = locationService.findNearbyLandmarksByTerm(currentPosition.getQualifiedCoordinates(), searchCriteria);
			}else if(StringUtils.isNullOrEmpty(category)){
				places = locationService.findNearbyLandmarks(currentPosition.getQualifiedCoordinates());
			}else{
				places = locationService.findNearbyLandmarks(currentPosition.getQualifiedCoordinates(), category);
			}

			notifyTaskFinished(places);
		} catch (LocationException e) {
			notifyError(Messages.CURRENT_LOCATION_NOT_FOUND + e.getMessage());
		} catch (InterruptedException e) {
			notifyError(Messages.DISPLAY_PLACES_ERROR);
		} catch (IOException e) {
			notifyError(Messages.ERROR_READING_RECORDS);
		} catch (SecurityException e) {
			notifyError(Messages.CURRENT_LOCATION_NOT_FOUND + e.getMessage());
		}
	}

	public void setCategory(String category) {
		this.category = category;
		this.searchCriteria = null;
	}

	public void setSearchCriteria(String searchCriteria) {
		this.searchCriteria = searchCriteria;
		this.category = null;
	}

}
